
#include <stdio.h>

#include "dua_def.h"
#include "dua_lex.h"
#include "dua_opcode.h"
#include "dua_proto.h"
#include "dua_order.h"


static const char* ASM = \
"= int 1\n" \
"= int 2\n" \
".load      0   0   0\n" \
".load      1   0   0\n" \
".addi      0   1   0\n" \
".castb     2   0   0\n" \
".end       0   0   0";


static void
printValue(DuaValue* v) {
    switch (v->type) {
    case DUA_TBOOL:
        printf("%s:bool", v->b ? "true" : "false"); break;
    case DUA_TINT:
        printf("%d:int", v->i); break;
    case DUA_TLONG:
        printf("%lld:long", v->l); break;
    case DUA_TSTRING:
        printf("%s:string", v->ts->s); break;
        break;
    }
}

static void
printStack(DuaState* D) {
    DuaValue* v;
    for (unsigned i = 0; i < D->top; i++) {
        v = &D->slot[i];
        printf("=%d ", i);
        printValue(v);
        printf("\n");
    }
}

static unsigned
testLex(DuaState* D){

    char buffer[2048];
    const char* bcsPath = "F:\\project\\LandEngine\\dua\\test\\dua.bcs";
    FILE* bcsFile = fopen(bcsPath, "rb");
    int len = fread(buffer, 1, 2048, bcsFile);
    DuaProto* proto = duaLoadOrder(D, buffer, len);

    //DuaProto* proto = duaLoadProto(D, ASM, strlen(ASM));
    //DuaClosure* closure = duaNewObj(D, DUA_TCLOSURE, sizeof(DuaClosure));
    //closure->proto = proto;
    //duaDo(D, closure);
    //printStack(D);
    return 0;
}

int main() {

    DuaState* D = duaNewState(NULL, NULL);
    unsigned nResult;
    int ret = duaCallFunc(D, testLex, &nResult);
    printf("ret:%d\n", ret);
    if (ret) {
        printValue(&D->slot[D->top - 1]);
        printf("\n");
    }

    return 0;
}
